package com.yx.front.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.front.config.global.BizException;
import com.yx.front.config.global.CommonResult;
import com.yx.front.model.BookMessage;
import com.yx.front.model.param.BookMessageParam;
import com.yx.front.model.vo.BookMessageVo;
import com.yx.front.service.BookMessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static com.yx.front.converter.BookMessageConverter.getBookMessageVo;
import static com.yx.front.converter.BookMessageConverter.handleResult;

/**
 * @author YangXiong
 */
@Component
public class BookMessageManager {
    @Resource
    private BookMessageService bookMessageService;

    public BookMessageVo getDetail(Long id) {
        if(Objects.isNull(id)){
            throw new BizException("参数不能为空");
        }
        BookMessage bookMessage = bookMessageService.getById(id);
        return getBookMessageVo(bookMessage);
    }

    public CommonResult<List<BookMessageVo>> getBookMessageList(BookMessageParam param) {
        //动态参数 筛选条件
        LambdaQueryWrapper<BookMessage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(param.getKind()), BookMessage::getKind, param.getKind());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(param.getName()), BookMessage::getName, param.getName());

        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(param.getType()), BookMessage::getType, param.getType());
        Page<BookMessage> bookMessagePage = new Page<>(param.getCurrentPage(), param.getTotalPage());
        Page<BookMessage> page = bookMessageService.page(bookMessagePage, lambdaQueryWrapper);
        List<BookMessageVo> result = handleResult(page);
        return CommonResult.success(result, page.getTotal());
    }
}
